package com.qf.flash.ui.circle;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.flash.R;
import com.qf.flash.model.StrategyBean;
import com.qf.flash.mvp.contract.StrategyContract;
import com.qf.flash.mvp.presenter.StrategyPresenter;
import com.qf.flash.ui.home.adapter.CircleRecommendItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.LazyBaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 圈子模块  ---->  推荐
 */
public class RecommendFragment extends LazyBaseFragment implements StrategyContract.View, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.rv)
    RecyclerView mRecyclerView;
    Unbinder unbinder;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    private RVAdapter<Object> mAdapter;
    private StrategyPresenter strategyPresenter;
    private boolean isfresh,isLoadMore;
    private int num = 1, size = 30;
    @Override
    protected int setContentView() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initInject() {
        super.initInject();
        strategyPresenter = new StrategyPresenter();
        initPresenters(strategyPresenter);
    }

    @Override
    protected void initView() {
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
    }

    @Override
    protected void lazyLoad() {
        mAdapter = new RVAdapter<>(new CircleRecommendItem(getActivity()));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);

        strategyPresenter.getMethodList("", "1", num, size);

    }

    public static RecommendFragment newInstance() {
        RecommendFragment recommendFragment = new RecommendFragment();
        return recommendFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setStrategyListData(List<StrategyBean> strategyBeans,int totalPage) {
        mRefresh.setEnableLoadMore(num < totalPage);
        List<Object> datas = mAdapter.getDatas();

        if (isfresh) {
            mRefresh.finishRefresh();
            datas.clear();
        } else if(isLoadMore){
            mRefresh.finishLoadMore();
        }

        datas.addAll(strategyBeans);
        mAdapter.notifyDataSetChanged();
        isfresh = false;
        isLoadMore = false;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isfresh = true;
        num = 1;
        strategyPresenter.getMethodList("", "1", num, size);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isLoadMore = true;
        num++;
        strategyPresenter.getMethodList("", "1", num, size);
    }
}
